﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Data;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TWBFREE
    {
        public TWBFREE()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();

        } 
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }
        bool IsWB61 = false;
        
        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e);
            EventDispatcher.Instance.RaiseStatus("开始留位", StatusEventType.StopProgress);
            if (IsWB61)
            {
                TMV.AB05WB61(TMV.IsFreeWB);
            } 
        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            string key = ORDERNUM;
            WB61VVT = "";
            WB61VVT = key;
            IsWZChg = false;
            IsWZChg = true; 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWB = dwMaster;
            if (dwWB == null)
            {
                TAX = "";
            }
            else
            {
                TAX = dwWB["WBBTAX"].ToString();

            }
        }
        public void WBREQ(MessageInfo msg)
        {
            TBMain.Clear();
            TMV.WBREQ(msg);
            TBMain.AcceptChanges();
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("已处于编辑状态，请先将数据保存后才可再次进行编辑！");
                return;
            }

            MV.TBTemp = null;
            MV.TBTemp = TBMain.Copy();
            base.EditCommand(sender, e);
            IsEdit = true;
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            { 
                IsWB61 = true;
                WBROWS = new List<DataRow>();
                DataTable TB = TBMain.Clone();
                foreach (var item in dlgMaster.SelectedItems)
                {
                    if (!item.GetType().ToString().Contains("DataRowView")) continue;

                    DataRow _dwWB = ((System.Data.DataRowView)item).Row;

                    if (_dwWB["WBBAUFLFD"].ToString() != "000000")
                    {
                        continue;
                    }

                    dwData = TB.Rows.Add();
                    dwData.ItemArray = _dwWB.ItemArray;
                    dwData["WBBAUFTYP"] = AUFTYP;
                    dwData["WBBAUFKRS"] = KRS;
                    dwData["WBBAUFLFD"] = LFD;
                    dwData["WBBAUFPOS"] = POS;
                    dwData["WBBTP"] = "0001";
                    dwData["WBBAENDBEN"] = app.User;
                    dwData["WBBAENDDAT"] = MV.date;
                }
                foreach (DataRow _dwWB in TB.Rows)
                {
                    WBROWS.Add(_dwWB);
                    filter = "WBBSTKNR='" + _dwWB["WBBSTKNR"].ToString() + "'";
                    rows = TBMain.Select(filter);
                    foreach (DataRow row in rows)
                    {
                        row.ItemArray = _dwWB.ItemArray;
                    }
                }
                bgWorker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FreeQtyCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = "WBBAUFLFD='000000'";
        }

        private void LockedQtyCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = "WBBAUFKRS='" + KRS + "' AND WBBAUFLFD='" + LFD + "' AND WBBAUFPOS='" + POS + "'";
        }

        private void AllQtyCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
        }

        private void WBCUTCommand(object sender, RoutedEventArgs e)
        {
            if (dwWB == null)
                return;
            if (dwWB["WBBAUFLFD"].ToString() != "000000")
            {
                MessageBox.Show("已经被留位，需要先取消留位");
                return;
            }
            VICC_WFEVENTS.SVWF.WB10(RUNID, dwWB["WBBSTKNR"].ToString());
        }

  

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            TMV.BATCHWBCHG();
            base.SaveCommand(sender, e);
            IsEdit = false;
        }

        private void WB4XCommand(object sender, RoutedEventArgs e)
        {
            if (dwWB == null)
                return;
            if (dwWB["WBBAUFLFD"].ToString() != "000000")
            {
                MessageBox.Show("已经被留位，需要先取消留位");
                return;
            }
            VICC_WFEVENTS.SVWF.WB10(RUNID, dwWB["WBBSTKNR"].ToString());
        }

        public override void OnEXAUFChg(string vlaue)
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            AUFTYP = EXAUF.Substring(63, 1);
            KRS = EXAUF.Substring(64, 2);
            LFD = EXAUF.Substring(66, 6);
            POS = EXAUF.Substring(72, 3);
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBWAKZ"] = EXAUF.Substring(0, 1);
            dwData["WBBART"] = EXAUF.Substring(1, 20);
            dwData["WBBVAR"] = EXAUF.Substring(21, 2);
            dwData["WBBFARBE"] = EXAUF.Substring(23, 10);
            dwData["WBBDESS"] = EXAUF.Substring(43, 10);
            dwData["WBBCOLORIT"] = EXAUF.Substring(53, 10);
            dwData["WBBFILLER"] = " AND WBBAUFLFD='000000'";            
            dwData["WBBLO"] = LO;
            dwData["WBBLB"] = LB;
            SResult = TMV.QWB10(dwData);
            MergeData(SResult, true, true);
            TBMain.AcceptChanges();
            base.OnEXAUFChg(vlaue);
            if(TBMain.Rows.Count>0)
            {
                //cvMaster.CustomFilter = "WBBAUFKRS='" + KRS + "' AND WBBAUFLFD='" + LFD + "' AND WBBAUFPOS='" + POS + "'"; 
            }
        }

    }
}
